# import cv2
# img = cv2.imread(r"D:\l'j't\code\pycharm\learning-python\AI\images\005.jpeg")
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#
# face = cv2.CascadeClassifier(r"D:\l'j't\code\pycharm\learning-python\AI\haarcascade_eye.xml")
#
# eye = cv2.CascadeClassifier(r"D:\l'j't\code\pycharm\learning-python\AI\haarcascade_frontalface_default.xml")
#
# faces = face.detectMultiScale(gray)
#
# for x, y, w, h in faces:
#     cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#     roi_eye = gray[y : y + h, x : x + w]
#     eyes = eye.detectMultiScale(roi_eye)
#     for (ex, ey, ew, eh) in eyes:
#         cv2.circle(img[y : y + h, x : x + w], (int(ex + ew / 2), int(ey + eh / 2)),
#                    int(max(ew, eh / 2)), (0, 255, 0), 2)
# cv2.imshow("face", img)
# cv2.waitKey(0)

import cv2

img = cv2.imread(r"D:\l'j't\code\pycharm\learning-python\AI\images\003.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

face_cascade = cv2.CascadeClassifier(r"D:\l'j't\code\pycharm\learning-python\AI\haarcascade\haarcascade_frontalface_default.xml")  # 👈 人脸检测器
eye_cascade = cv2.CascadeClassifier(r"D:\l'j't\code\pycharm\learning-python\AI\haarcascade\haarcascade_eye.xml")

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

for x, y, w, h in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)  # 用蓝色框出人脸
    roi_gray = gray[y:y + h, x:x + w]
    roi_color = img[y:y + h, x:x + w]

    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex, ey, ew, eh) in eyes:
        cv2.circle(roi_color, (int(ex + ew / 2), int(ey + eh / 2)), int(max(ew, eh) / 2), (0, 255, 0), 2)  # 用绿色圆圈标出眼睛中心

cv2.imshow("Detected Faces and Eyes", img)
cv2.waitKey(0)
cv2.destroyAllWindows()